
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:小工具活动标签信息协议类
 *
 * @author jerryliu
 * @since 2016年5月16日 下午4:19:32
 */
public class FloatViewActivityLabelTask {
    private ArrayList<String> mCmdList = new ArrayList<String>();

    public FloatViewActivityLabelResponse request() {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();

        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        FloatViewActivityLabelResponse respObj = new FloatViewActivityLabelResponse();
        ResponsePackage resp = new ActivityLabelResponsePackage();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_FLOATVIEW_ACTIVITY_LABEL);
        requestParams.add(params);
        req.setRequestParams(requestParams);
        mCmdList.add(ProtocolCmd.CMD_FLOATVIEW_ACTIVITY_LABEL);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (Exception e) {
        }
        return null;
    }

    class ActivityLabelResponsePackage extends
            HttpBaseResponsePackage<FloatViewActivityLabelResponse> {

        @Override
        protected void handleResponse(FloatViewActivityLabelResponse response,
                String responseJsonStr) {
            try {
                LogUtil.d("response", "FloatViewActivityLabelTask:" + responseJsonStr);
                JSONArray responseArray = new JSONArray(responseJsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.optJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    // 根据cmd解析每个请求
                    if (ProtocolCmd.CMD_FLOATVIEW_ACTIVITY_LABEL.equalsIgnoreCase(cmd)) {
                        String code = jsonResp.optString("code");
                        String message = jsonResp.optString("message");
                        String prompt = jsonResp.optString("prompt");

                        response.setCode(code);
                        response.setMessage(message);
                        response.setPrompt(prompt);

                        if (jsonResp.optInt("code") != 1) {
                            continue;
                        }
                        if (jsonResp.isNull("data")) {
                            continue;
                        }

                        JSONObject jsonResult = jsonResp.optJSONObject("data");
                        if (jsonResult != null) {
                            response.setLableContent(jsonResult.optString("content"));
                            response.setLableImageUrl(jsonResult.optString("image"));
                            response.setOk(true);
                        }
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    public static class FloatViewActivityLabelResponse extends ResponseMessage {

        private static final long serialVersionUID = -4820735216365527465L;

        private String lableContent;

        private String lableImageUrl;

        public String getLableContent() {
            return lableContent;
        }

        public void setLableContent(String lableContent) {
            this.lableContent = lableContent;
        }

        public String getLableImageUrl() {
            return lableImageUrl;
        }

        public void setLableImageUrl(String lableImageUrl) {
            this.lableImageUrl = lableImageUrl;
        }
    }
}
